var P5 = function (game) {
  this.create = function () {


    ClosePage(game, '5_0', '5', '5_r', '5_w');

    function Page(game, cbg, mbg, rbg, wbg) {
      var bg = game.add.sprite(0, 0, imgs['5']);
      bg.anchor.setTo(0, 0);

      var group = game.add.group();

      flagAry=[];
      var topX = [530, 713, 896, 1079];
      var btmX = [530, 717, 910, 1100];

      var btnTop = [480, 660, 830, 1010];
      var point = [];
      var cupimgs=['5_00','5_1','5_2','5_3','5_4','5_5','5_6','5_7'];
      var tween;
      var button;
      var t = null, b = null;
      var graphics = game.add.graphics(0, 0);


      for (var i = 0; i < cupimgs.length; i++) {
        if(i<4) {
            button = game.add.button(btnTop[i], 320 - 180, cupimgs[i], actionOnClick, this, 2, 1, 0);
        }else{
            button = game.add.button(btmX[i-4] - 90, 410, cupimgs[i], actionOnClick, this, 2, 1, 0);
        }
        button.data = i;
      }



      graphics.lineStyle(10, 0xffd900, 1);

      function actionOnClick(arg) {
        if (arg.data < 4) {
          t = arg.data;
          if (b != null) {
            //point = filters([t, b], point);
            point.push([t, b]);
            t = null;
            b = null;
          }
        }
        if (arg.data > 3) {
          b = arg.data;
          if (t != null) {
            //point = filters([t, b], point);
            point.push([t, b]);
            t = null;
            b = null;
          }
        }

        tween = game.add.tween(arg).to({x: arg.x-5, y: arg.y}, 100, Phaser.Easing.Linear.None, true,0,3);

        graphics.clear();
        graphics.lineStyle(2, 0x48486C, 1);
        point.forEach( function(item, k){
          graphics.moveTo(topX[item[0]], 320); //把画笔移动到指定的坐标
          graphics.lineTo(btmX[item[1] - 4], 405);  //绘制一条从当前位置到指定坐标(200, 50)的直线.

        });

      }


      var retry = game.add.button(1080, 625, imgs['button'], onSubmit, this, 2, 1, 0);
      retry.name = 'retry';
      retry.width = 160;
      retry.height = 55;

      var submit = game.add.button(1250, 625, imgs['button'], onSubmit, this, 2, 1, 0);
      submit.name = 'submit';
      submit.width = 160;
      submit.height = 55;


      function onSubmit(arg) {
        var flag=true;
        point.forEach(function (item,i) {
          if(item[0] ==0 && item[1]!=7){
            flag=false;
          }
          if(item[1] ==1 && item[1]!=4){
            flag=false;
          }
          if(item[0] ==2 && item[1]!=5){
            flag=false;
          }
          if(item[0] ==3 && item[1]!=6){
            flag=false;
          }
        })
        if(point.length <4){
          flag=false;
        }
        if (arg.name === 'submit') {
          if (flag) {
            RightPage(game, cbg, mbg, rbg, wbg);
          } else {
            WrongPage(game, cbg, mbg, rbg, wbg);
          }
        } else {
          game.goState('back');
        }
      }

    }

    function ClosePage(game, cbg, mbg, rbg, wbg) {
      var container = game.add.sprite(0, 0, '');
      var close = game.add.button(0, 0, cbg, onClose, this, 2, 1, 0);
      container.addChild(close);

      function onClose() {
        container.destroy();
        Page(game, cbg, mbg, rbg, wbg);
      }
    }

      function WrongPage(game,cbg,mbg,rbg,wbg){
          var container=game.add.sprite(0,0,'');

          var bg = game.add.image(0, 0, wbg);
          container.addChild(bg);
          var retry = game.add.button(1250, 625, imgs['button'], onClick, this, 2, 1, 0);
          retry.width = 160;
          retry.height = 55;
          container.addChild(retry);

          function onClick() {
              container.destroy();
              Page(game, cbg, mbg, rbg, wbg);
          }
      }

    function RightPage(game, cbg, mbg, rbg, wbg) {
      var container = game.add.sprite(0, 0, '');

      var bg = game.add.image(0, 0, rbg);
      container.addChild(bg);
      var next = game.add.button(1250, 625, imgs['button'], onClick, game, 2, 1, 0);
      next.width = 160;
      next.height = 55;
      container.addChild(next);

      function onClick() {
        game.goState('next');
      }
    }
  }


}

